package com.msb.servicemap.remote;

import com.msb.commons.constant.AmapConstant;
import com.msb.commons.response.ResponseResult;
import com.msb.commons.response.TerminalResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chshh
 * @version: 1.0
 */
@Service
public class TerminalClient {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${amap.key}")
    private String amapKey;

    @Value("${amap.sid}")
    private String amapSid;

    public ResponseResult add(String name,String desc) {

        StringBuilder urlBuilder = new StringBuilder();

        urlBuilder.append(AmapConstant.TERMINAL_ADD_URL);
        urlBuilder.append("?");
        urlBuilder.append("key=" + amapKey);
        urlBuilder.append("&");
        urlBuilder.append("sid=" + amapSid);
        urlBuilder.append("&");
        urlBuilder.append("name=" + name);
        urlBuilder.append("&");
        urlBuilder.append("desc=" + desc);
        System.out.println("urlBuilder = " + urlBuilder);
        //调用高德接口
        ResponseEntity<String> entity = restTemplate.postForEntity(urlBuilder.toString(), null, String.class);

        //解析接口
        /*{
            "data": {
                    "name": "车辆测试",
                    "tid": 591474943,
                    "sid": 817116
        },
            "errcode": 10000,
                "errdetail": null,
                "errmsg": "OK"
        }*/
        String body = entity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);

        return ResponseResult.success(terminalResponse);
    }

    public ResponseResult<ArrayList<TerminalResponse>> aroundsearch(String center, int radius) {

        StringBuilder urlBuilder = new StringBuilder();

        urlBuilder.append(AmapConstant.TERMINAL_AROUNDSEARCH_URL);
        urlBuilder.append("?");
        urlBuilder.append("key=" + amapKey);
        urlBuilder.append("&");
        urlBuilder.append("sid=" + amapSid);
        urlBuilder.append("&");
        urlBuilder.append("center=" + center);
        urlBuilder.append("&");
        urlBuilder.append("radius=" + radius);

        //调用高德接口
        ResponseEntity<String> entity = restTemplate.postForEntity(urlBuilder.toString(), null, String.class);

        //解析接口
/*        {
            "data": {
                    "count": 1,
                    "results": [
                {
                "createtime": 1667701077426,
                 "desc": "1562322854910889994",
                 "locatetime": 1666756375523,
                 "location": {
                        "accuracy": 550,
                        "direction": 511,
                        "distance": 0,
                        "height": null,
                        "latitude": 39.90923,
                        "longitude": 116.397428,
                        "speed": 255,
                        "trackProps": null
                 },
                "name": "皖P76123",
                "props": null,
                "tid": 596224061
            }
        ]
        },
        "errcode": 10000,
        "errdetail": null,
        "errmsg": "OK"
        }*/
        String body = entity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray results = data.getJSONArray("results");

        ArrayList<TerminalResponse> terminalResponseList = new ArrayList<>();
        TerminalResponse terminalResponse = new TerminalResponse();

        for (int i = 0; i <results.size() ; i++) {
            JSONObject object = results.getJSONObject(i);
            //desc是carId
            String desc = object.getString("desc");
            Long carId = Long.parseLong(desc);
            String tid = object.getString("tid");

            JSONObject location = object.getJSONObject("location");
            String longitude = location.getString("longitude");
            String latitude = location.getString("latitude");

            terminalResponse.setCarId(carId);
            terminalResponse.setTid(tid);
            terminalResponse.setLongitude(longitude);
            terminalResponse.setLatitude(latitude);

            terminalResponseList.add(terminalResponse);
        }

        return ResponseResult.success(terminalResponseList);
    }

}
